const title = require('./title')
const request = require('./request')

const syncUser = async (info) => {
    const level = info.level
    let job = title.称号转职业(info.job)
    let exp = 0
    let gap = 0
    if (level !== 160) {
        exp = info.xp
        gap = info.maxxp - info.xp
    } 
    const data = {
        name: info.name,
        job: job,
        level: level,
        exp: exp,
        gap: gap,
        money: info.gold,
    }
    console.log(data)
    const option = {
        method: 'POST',
        body: JSON.stringify(data),
    }
    return request.request('/api/v1/account/user/sync/', option)
            .then(data => {
                    console.log(data)
                    if (data.code === 0) {
                        console.log('同步角色完成')
                    }
                }
            )
}

const syncUserBag = (info) => {
    let goods_list = []
    for (let i in info.goods_list) {
        let count = info.goods_list[i].count
        if (count === 0) {
            count = 1
        }
        goods_list.push({
            name: info.goods_list[i].name,
            sign: info.goods_list[i].itemid,
            count: count,
        })
    }
    let pet_list = []
    for (let i in info.pet_list) {
        pet_list.push({
            name: info.pet_list[i].name,
            real_name: info.pet_list[i].realname,
            sign: info.pet_list[i].flags,
            level: info.pet_list[i].level,
        })
    }
    const data = {
        name: info.name,
        goods_list: goods_list,
        pet_list: pet_list,
    }
    const option = {
        method: 'POST',
        body: JSON.stringify(data),
    }
    return request.request('/api/v1/account/user/bag/sync/', option)
            .then(
                data => {
                    if (data.code === 0) {
                        console.log('同步角色背包完成')
                    }
                }
            )
}

const syncUserBank = (info) => {
    let goods_list = []
    for (let i in info.goods_list) {
        let count = info.goods_list[i].count
        if (count === 0) {
            count = 1
        }
        goods_list.push({
            name: info.goods_list[i].name,
            sign: info.goods_list[i].itemid,
            count: count,
        })
    }
    let pet_list = []
    for (let i in info.pet_list) {
        pet_list.push({
            name: info.pet_list[i].name,
            real_name: info.pet_list[i].realname,
            sign: info.pet_list[i].flags,
            level: info.pet_list[i].level,
        })
    }
    const data = {
        money: info.money,
        name: info.name,
        goods_list: goods_list,
        pet_list: pet_list,
    }
    const option = {
        method: 'POST',
        body: JSON.stringify(data),
    }
    return request.request('/api/v1/account/user/bank/sync/', option)
            .then(
                data => {
                    if (data.code === 0) {
                        console.log('同步角色银行完成')
                    }
                }
            )
}

module.exports = {
  syncUser,
  syncUserBag,
  syncUserBank,
}